package com.vip.service.admin.impl;

import com.alibaba.fastjson.JSON;
import com.vip.base.domain.config.util.RedisUtil;
import com.vip.dao.admin.dao.UserDao;
import com.vip.dao.admin.domain.User;
import com.vip.service.admin.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Autowired
    RedisUtil redisUtil;


    @Override
    public User findUserById(Long id) {
        String key = "user_"+id;


        //判断redis中是否存在键为key的缓存
        boolean hashKey = redisUtil.hasKey(key);
        if(hashKey){
            String str = JSON.toJSON(redisUtil.get(key)).toString();
            User user = JSON.parseObject(str,User.class);
            System.out.println("从缓存中得到数据 " + user.getUserName());
            System.out.println("-------------------------------");
            return user;
        }else{
            Optional<User> optional = userDao.findById(id);
            if(!optional.isPresent()){
                return null;
            }
            User user = optional.get();
            System.out.println("查询数据库中的数据： " + user.getUserName());
            System.out.println("---------------------");
            redisUtil.set(key,user,3000);
            return user;
        }

    }

    @Override
    public Optional<User> findUserById2(Long id) {
        Optional<User> optional = userDao.findById(id);
        return optional;
    }



    @Override
    public User addUser(User user) {
        return userDao.save(user);
    }

    @Override
    public User findUserByUserName(String userName) {
        return userDao.findUserByUserName(userName);
    }

    @Override
    public User findUserByUserNameAndPassword(String userName, String passWord) {
        String key1 = userName+"-"+passWord;
        boolean hashKey = redisUtil.hasKey(key1);
        if(hashKey){
            Object obj =  redisUtil.get(key1);
            String str = JSON.toJSONString(obj);
            System.out.println("redis---------》从缓存中拿数据");
            return JSON.parseObject(str,User.class);
        }
        System.out.println("redis----------》从数据库中拿数据");
        User user = userDao.findUserByUserNameAndPassWord(userName,passWord);
         redisUtil.set(key1,user,3000);
        return user;
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public Page<User> findAlluser(int page, int size){
        Pageable pageable = PageRequest.of(page,size);
        Page<User> userPage = userDao.findAll(pageable);
        return userPage;
    }

    @Override
    public void deleteUserById(Long id) {
        userDao.deleteById(id);
    }
}
